package org.example.studentsystem_back.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Dorm;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.service.DormService;
import org.example.studentsystem_back.vo.dormMangeVo.DormVo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Valid
@RestController
public class DormController {
    @Resource
    DormService dormService;

    @PostMapping("/dorm")
    public CommonResponse<DormVo> getDormAll(@RequestBody String  json)
    {
        JSONObject jsonObject = JSON.parseObject(json);
        PageDTO pageDTO=jsonObject.toJavaObject(PageDTO.class);
        String a=jsonObject.getString("id");
        int id;
        if(a==null||a.equals("")){
            id=-1;
        }
        else {
            id=Integer.parseInt(a);
        }
        String name=jsonObject.getString("name");
        return dormService.findAll(pageDTO.getPage(),pageDTO.getLimit(),name,id);
    }

    @PostMapping("/dorm/add")
    public CommonResponse<Dorm> add(@Valid @RequestBody Dorm dorm)
    {
        return dormService.add(dorm);
    }

    @PostMapping("/dorm/update")
    public CommonResponse<Dorm> update(@Valid @RequestBody Dorm dorm)
    {
        return dormService.update(dorm);
    }

    @PostMapping("/dorm/delete")
    public CommonResponse<Dorm> delete(@Valid @RequestBody Dorm dorm){
        return dormService.delete(dorm);
    }
}
